﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ArchiveLink
{
    internal class SyncerFactory
    {
        private readonly FileInfo _mockFile;

        public SyncerFactory(FileInfo mockFile)
        {
            _mockFile = mockFile;
        }

        public ISyncer GetSyncerImpl(string source)
        {
            var extension = Path.GetExtension(source).Trim('.').ToLowerInvariant();
            switch (extension)
            {
                case "xml":
                case "nfo":
                case "jpg":
                case "jpeg":
                case "png":
                case "disc":
                    return new CopySyncer(new FileInfo(source));
                default:
                    return new MockFileSyncer(_mockFile);
            }
         }
    }
    internal interface ISyncer
    {
        void CreateMirror( string linkPath);
    }

    internal class MockFileSyncer:ISyncer
    {
        private readonly FileInfo _mockFile;

        public MockFileSyncer(FileInfo mockFile)
        {
            _mockFile = mockFile;
        }

        public void CreateMirror(string linkPath)
        {
            DirectoryLinker.CreateFileLink(_mockFile,linkPath);
        }
    } 
    internal class CopySyncer:ISyncer
    {
        private readonly FileInfo _source;

        public CopySyncer(FileInfo source)
        {
            _source = source;
        }

        public void CreateMirror(string linkPath)
        {
            File.Copy(_source.FullName,linkPath, true);
        }
    }    
    internal class HeaderSyncer:ISyncer
    {
        private readonly FileInfo _source;
        private int _copyLength=1024*1024;
        

        public HeaderSyncer(FileInfo source)
        {
            _source = source;
        }

        public void CreateMirror(string linkPath)
        {
            var src = _source.OpenRead();
            var tgt = File.Open(linkPath, FileMode.CreateNew, FileAccess.Write);
            var buffer = new byte[_copyLength];
            var bytesRead=src.Read(buffer, 0, _copyLength);
            tgt.Write(buffer,0,bytesRead);
            src.Close();
            tgt.Close();
        }
    }
}
